//
//  HWpinglunxiangqingshituView.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/9/2.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWpinglunxiangqingshituView.h"
#import "HWpinglunFrame.h"
#import "HWpinglunHeaderView.h"
#import "HWpinglunfootView.h"
#import "HWNewCommentsTableViewCell.h"
#import "HWpinglunxiangqingshituView.h"
#import "HWLoginController.h"

#define  HZKScreenH  self.view.frame.size.height

#define  HZKScreenW  self.view.frame.size.width

@interface HWpinglunxiangqingshituView ()<UITableViewDataSource,UITableViewDelegate,HWpinglunHeaderViewdelegate,HWpinglunfootViewDelegate>
@property(strong,nonatomic) UITableView *maintablview;
@property(strong,nonatomic)NSMutableArray *dataMuArr;
@property(strong,nonatomic) UIButton *sendBtn;
@property(strong,nonatomic) UITextField *commentTF;
@property(strong,nonatomic) UIView *commentView;
@property(strong,nonatomic) UIView *tongmingview;
@property(strong,nonatomic) HWpinglunFrame *frameModel;



@end

@implementation HWpinglunxiangqingshituView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];
    
    self.navigationItem.title = @"回复";
    self.view.backgroundColor = [UIColor whiteColor]; 
    [self detailsInterfaceCommentsxiangqing];
    
    [self noticeshituView];
    
    [self addviewjianpan];
    
    
    
    
}




- (void)noticeshituView {
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}


- (void)addviewjianpan {
    
    UIView *tongmingview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    tongmingview.backgroundColor = [UIColor clearColor];
    self.tongmingview = tongmingview;
    self.tongmingview.hidden = YES;
    [self.view addSubview:tongmingview];
    
   
    
    
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shituxianshi)];
    
    [tongmingview addGestureRecognizer:tap1];
    
  
    
    UIView *keyBoardTopView = [[UIView alloc] initWithFrame:CGRectMake(0, HZKScreenH-60, HZKScreenW, 60)];
     keyBoardTopView.backgroundColor = [UIColor whiteColor];
   //   keyBoardTopView.layer.borderWidth = 0.7;
   //   keyBoardTopView.layer.borderColor = HZKColor(204, 204, 204).CGColor;
     //发布按钮
     self.sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(keyBoardTopView.bounds.size.width - 60+10, 10, 40, 40)];
     self.sendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
     [self.sendBtn setTitle:@"发布" forState:UIControlStateNormal];
    //  self.sendBtn.alpha = 0.4;
     self.sendBtn.tag = 2002;
    self.sendBtn.layer.cornerRadius = 5;
    self.sendBtn.layer.masksToBounds = YES;
    
     [self.sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.sendBtn.backgroundColor =RGB(247, 124, 47);
    
     [self.sendBtn addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside];
     [keyBoardTopView addSubview:self.sendBtn];
     //输入框
     UITextField *inputTF = [[UITextField alloc] init];
     inputTF.frame = CGRectMake(10, 12, HZKScreenW - 10 - 60, 36);
     inputTF.placeholder = @"把你想说的写在这里";
     inputTF.tag = 1001;
     inputTF.layer.cornerRadius = 5;
     inputTF.layer.masksToBounds = YES;
     self.commentTF = inputTF;
   //   inputTF.backgroundColor = HZKColor(242, 242, 242);
     inputTF.backgroundColor = [UIColor colorWithRed:242.0/255.0 green:242.0/255.0 blue:242.0/255.0 alpha:242.0/255.0];
    
     [inputTF addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
     [keyBoardTopView addSubview:inputTF];
     self.commentView = keyBoardTopView;
     [self.view addSubview:keyBoardTopView];
 //    [tongmingview addSubview:keyBoardTopView];
    
    
    UIView *zuixiamian = [[UIView alloc] initWithFrame:CGRectMake(0,  HZKScreenH-60, HZKScreenW, 60)];
    zuixiamian.backgroundColor = [UIColor clearColor];
    [self.view addSubview:zuixiamian];
    UITapGestureRecognizer *shiyutapViw = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zuixiamianTap)];
    [zuixiamian  addGestureRecognizer:shiyutapViw];
    
    
    
    
    
}


- (void)zuixiamianTap {
    
    self.commentTF.placeholder =  [NSString stringWithFormat:@"把你想说的写在这里"];
    self.frameModel = nil;
    [self.commentTF becomeFirstResponder];
    
    
}


- (void)shituxianshi {
    
    [self.view endEditing:YES];
    //  self.tongmingview.hidden = YES;
    
     
}



- (void)buttonClick{
   //  [self presentViewController:[[KXIncomeDetailController alloc] init] animated:YES completion:nil];
}



#pragma mark -键盘监听方法
- (void)keyBoardWillShow:(NSNotification *)notification
{
//     获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notification.userInfo];
    // 获取键盘高度
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
 
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.commentView.transform = CGAffineTransformMakeTranslation(0, -(keyBoardHeight));
    };
 
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
    
    
    self.tongmingview.hidden = NO;
    
}
- (void)keyBoardWillHide:(NSNotification *)notificaiton
{
    
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notificaiton.userInfo];
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
 
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.commentView.transform = CGAffineTransformIdentity;
    };
 
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
    
    
    self.tongmingview.hidden = YES; 
}




- (void)textFieldEditChanged:(UITextField *)sender {
    
    NSLog(@"1kkfkfkkf");
    
    
    
    
}


- (void)dismissKeyboard:(UIButton *)sender {
    
    [self fasongqingqiu];
    
    
    
}








- (void)setUI {

   // self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  1000) style:UITableViewStylePlain];
    UIView *tablebheadeview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 10)];
    tablebheadeview.backgroundColor = RGB(245, 245, 245);
    
    self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  kScreenH-60) style:UITableViewStyleGrouped];
    self.maintablview.delegate = self;
    self.maintablview.dataSource = self;
    self.maintablview.tag = 10;
    self.maintablview.scrollEnabled = YES;
    self.maintablview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.maintablview.tableFooterView = [[UIView alloc]init];
    self.maintablview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.maintablview];
    
    self.maintablview.tableHeaderView =tablebheadeview;
    
    
    
    
    
}



// 获取子评论
- (void)detailsInterfaceCommentsxiangqing {
    
    // 斗
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    
    
    NSDictionary   *parametes = @{
                                              @"uid" : userID,
                                              @"ios" : @"1",
                                              @"ct" :  @"app2021",
                                              @"pid":  self.idStr,
                                              @"ac" : @"gameinfo_pllistson"
                                              };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
          
         
          
          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
          [MBProgressHUD hideHUD];
        
          if ([codeStr intValue] == 1) {
                NSLog(@"%@",response);

              
              self.dataMuArr = [[NSMutableArray alloc] init];
              
              NSMutableArray *arr0  =  [HWpinglunModel mj_objectArrayWithKeyValuesArray: response[@"data"]];
              
              if (arr0== nil) {
                  
                self.navigationItem.title =  [NSString stringWithFormat:@"%ld条回复",0];
                [self.maintablview reloadData];
                  
                  return;
              }
              
              NSMutableArray *arr  =  [HWpinglunModel mj_objectArrayWithKeyValuesArray: response[@"data"][0][@"son_list"]];
               
             //  NSMutableArray *arr0  =  [HWpinglunModel mj_objectArrayWithKeyValuesArray: response[@"data"]];
              HWpinglunFrame *mode =  [[HWpinglunFrame alloc]init];
              mode.datamodel = arr0[0];
              [self.dataMuArr addObject: mode];
              
              for (int i = 0; i < arr.count; i++) {
                  
                  HWpinglunFrame *mode =  [[HWpinglunFrame alloc]init];
                  mode.datamodel = arr[i];
                  [self.dataMuArr addObject: mode];
                
              }
              
              self.navigationItem.title =  [NSString stringWithFormat:@"%ld条回复",arr.count];
              
             
            [self.maintablview reloadData];
              
        
          } else {
              
              
              [MBProgressHUD showError:response[@"message"]];
              
          }
       
          
          
      } failure:^(NSString *error) {
        
          
      }];
    
    
    
    
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  self.dataMuArr.count;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 0;
    HWpinglunFrame *frame =     self.dataMuArr[section];
    return     [frame.datamodel.son_list count];
    
    
    
    return  1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    

    static NSString *cellID = @"celiD";
     HWNewCommentsTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
    
     if (cell == nil ) {
        cell =  [[HWNewCommentsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
     }
    
     HWpinglunFrame *frame =     self.dataMuArr[indexPath.section];
     HWpinglunModel *model =      frame.datamodel.son_list[indexPath.row];
     cell.frameModel = frame;
     cell.model = model ;
     cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
   
    
    
     return  cell;
  
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    HWpinglunFrame *frame =     self.dataMuArr[indexPath.section];
    HWpinglunModel *model =      frame.datamodel.son_list[indexPath.row];
    
    
     NSString  *content = [NSString stringWithFormat:@"%@:%@",model.user_name, model.content];
    
    
    
    if (model.beihuifu_user_name == nil  ) {
        
        content = [NSString stringWithFormat:@"%@:%@",model.user_name, model.content];
        
         
    } else {
        
        content = [NSString stringWithFormat:@"%@回复%@:%@",model.user_name,model.beihuifu_user_name ,model.content];
        
    }
   
    
    
    
    
     
  //   content = @"四川成家长把孩子送进校园后，纷纷贴墙“暗中观察”。9月1日，全国多地迎来都一小学开学，家长把孩子送进校后，纷纷贴墙“暗中";
    
    CGSize   size2 =    [WMUtil sizeWithFont1:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(kScreenW-60-15-20, 2000) string:content];
    CGFloat a=  10;
  //   self.neirongtextview.text = content;
    if (size2.height < 17) {
        return   size2.height+a ;
        
    } else {
        // a=  -10;
        return   size2.height+a;
        
    }
    
   
    return  150;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

        
     HWpinglunFrame *frame =     self.dataMuArr[section];
     return   frame.sectionHeight;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    
         
        return  36;
     
    
  
    
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
   
        HWpinglunHeaderView *view = [HWpinglunHeaderView headFooterViewWithTableview:tableView];
        // view.frame = CGRectMake(0, 0, kScreenW, 100);
        view.model = self.dataMuArr[section];
        view.xiangqingModel = self.dataMuArr[section];
        view.section = section;
        view.delegate = self;
        view.backgroundColor = [UIColor redColor];
    
    if (section == 0 ) {
        
    } else {
        
        for (int i = 0; i < 5 ; i++) {
             
             UIImageView *imageview =  view.storeStarsMuArr[i];
             imageview.hidden = YES;
         }
       
    }
    
        
       
 
        return view;
        
   
  
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    

        
        HWpinglunfootView *view = [HWpinglunfootView headFooterViewWithTableview:tableView];
        // view.frame = CGRectMake(0, 0, kScreenW, 100);
        view.model = self.dataMuArr[section];
        view.delegate = self;
    
        view.backgroundColor = [UIColor redColor];
      if (section == 0 ) {
        
        view.pinglunBut.hidden = NO;
         
    } else {
        
        view.pinglunBut.hidden = YES;
        
        
    }
        
    if (section == 0 ) {
        
        view.fenggexiangView.hidden = YES;
        view.fengeImageView.hidden = NO;
        
         
    } else {
        
        view.fenggexiangView.hidden = NO;
        view.fengeImageView.hidden = YES;
        
        
    }
    
    
    
        return  view;
        
   
    
    
}


- (void)HWpinglunfootView:(HWpinglunFrame *)modeFrame  sectionInt:(NSInteger)section {
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        HWLoginController *login = [[HWLoginController alloc]init];
        [self.navigationController pushViewController:login animated:YES];
     
        
        return ;
        
    }
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int t = [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];

    NSDictionary *parametes = @{
                                @"ct" : @"app2021",
                                @"ac" : @"pinglun_zan",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"sign":signMD5,
                                @"id":modeFrame.datamodel.id
                                };
    
    
     //  @"images" :ico,
    
    [MBProgressHUD showMessage:@"正在点赞..."];
    [HWWebUtils doPostWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           [MBProgressHUD hideHUD];
             
             
             if ([codeStr intValue] == 1) {
                 // [self.navigationController popViewControllerAnimated:YES];
               //    [MBProgressHUD  showError:@"发表评论成功"];
                 [self detailsInterfaceCommentsxiangqing];
                 

             } else {
                 
                  [MBProgressHUD  showError:response[@"message"]];
                 
             }
             NSLog(@"%@",response);
        
    } failure:^(NSString *error) {
         [MBProgressHUD  showError:@"加载失败"];
 
    }];
    
    
    
}



-(void)HWpinglunheadViewDelegate:(HWpinglunFrame *)modeFrame {
    
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"删除"
                                    message:@"确定要删除"
                                    preferredStyle:UIAlertControllerStyleActionSheet];
     
     UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                               //响应事件
          NSLog(@"action = %@", action);
         
         [self delegateshanchu:modeFrame];
         
         
      }];
     UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                               //响应事件
        NSLog(@"action = %@", action);
                                                        
     
     }];

     [alert addAction:defaultAction];
     [alert addAction:cancelAction];
     [self presentViewController:alert animated:YES completion:nil];
    
    
    
}


- (void)delegateshanchu:(HWpinglunFrame *)modeFrame {
    
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        HWLoginController *login = [[HWLoginController alloc]init];
        [self.navigationController pushViewController:login animated:YES];
     
        
        return ;
        
    }
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int t = [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
 //    NSString *signMD5 = [WMUtil md5HexDigest:sign];

    NSDictionary *parametes = @{
                                @"ct" : @"app2021",
                                @"ac" : @"dell_pinglun",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"id":modeFrame.datamodel.id
                                };
    
    
     //  @"images" :ico,
    
    [MBProgressHUD showMessage:@"正在删除..."];
    [HWWebUtils doPostWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           [MBProgressHUD hideHUD];
             
             
             if ([codeStr intValue] == 1) {
                 // [self.navigationController popViewControllerAnimated:YES];
               //    [MBProgressHUD  showError:@"发表评论成功"];
                 [self detailsInterfaceCommentsxiangqing];
                 

             } else {
                 
                  [MBProgressHUD  showError:response[@"message"]];
                 
             }
             NSLog(@"%@",response);
        
    } failure:^(NSString *error) {
         [MBProgressHUD  showError:@"加载失败"];
 
    }];
    
    
    
    
    
    
    
    
    
}




//- (void)HWpinglunHeaderView:(HWpinglunFrame*)frame {
//
//
////    HWpinglunxiangqingshituView *pilunxiang = [[HWpinglunxiangqingshituView alloc] init];
////    pilunxiang.pid = frame.datamodel.id;
////    [self.navigationController pushViewController:pilunxiang animated:YES];
//
//    self.frameModel = frame;
//
//    [self.commentTF becomeFirstResponder];
//
//
//
//
//
//
//
//
//
//
//
//}

- (void)HWpinglunHeaderViewSection:(NSInteger)section {
    
    
    if (section == 0 ) {
        
        
        
    } else {
        
        self.frameModel = self.dataMuArr[section];
        
        self.commentTF.placeholder =  [NSString stringWithFormat:@"回复@%@",self.frameModel.datamodel.user_name];
        
         [self.commentTF becomeFirstResponder];
        
        
    }
    
    
    
}


- (void)fasongqingqiu  {
    
    
    if (!(self.commentTF.text.length > 0)) {
      
        [MBProgressHUD showError:@"请输入内容"];
        return;
        
    }
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        HWLoginController *login = [[HWLoginController alloc]init];
        [self.navigationController pushViewController:login animated:YES];
       //  [self weidengluqingqiu];
        
        return ;
        
    }
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int t = [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    

    HWpinglunFrame *frammodel1 = self.dataMuArr[0];
    if (self.frameModel == nil) {
        
        
        HWpinglunFrame *frammodel1 = self.dataMuArr[0];
        
        self.pid = frammodel1.datamodel.id;
        
        self.ppid = frammodel1.datamodel.id;
        
        
    } else {
        
        
//        pinglun.pid =  self.model.id;
//        pinglun.ppid = self.frameModel.datamodel.id;
        
        self.pid =  self.frameModel.datamodel.id;
        self.ppid =frammodel1.datamodel.id;
        
        
        
    }

    
    NSDictionary *parametes = @{
                                @"ct" : @"app2021",
                                @"ac" : @"gameinfo_pinglun",
                                @"ios" : @"1",
                                @"id" :self.gidStr,
                                @"uid" :userID,
                                @"t" :tStr,
                                @"sign":signMD5,
                                @"content": self.commentTF.text,
                                @"pid":self.pid,
                                @"ppid":@"0",
                              
                                };
    
    if (self.ppid != nil ) {
           parametes = @{
                                    @"ct" : @"app2021",
                                    @"ac" : @"gameinfo_pinglun",
                                    @"ios" : @"1",
                                    @"id" :self.gidStr,
                                    @"uid" :userID,
                                    @"t" :tStr,
                                    @"sign":signMD5,
                                    @"content": self.commentTF.text,
                                    @"pid":self.pid,
                                    @"ppid":self.ppid,
                                  
                                    };
    }
    
    
     //  @"images" :ico,
    
    [MBProgressHUD showMessage:@"正在发布...."];
    [HWWebUtils doPostWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            //  [MBProgressHUD hideHUD];
             
             
             if ([codeStr intValue] == 1) {
                  
               //   [self detailsInterfaceCommentsxiangqing];
                 
                 self.commentTF.text  = @"";
                 
                 [self detailsInterfaceCommentsxiangqing];
                 [self.view endEditing:YES];
    

             } else {
                 
                  [MBProgressHUD  showError:response[@"message"]];
                 
             }
             NSLog(@"%@",response);
        
    } failure:^(NSString *error) {
         [MBProgressHUD  showError:@"加载失败"];
 
    }];
    
    
    
  
    
    
}





@end
